package com.aishua.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.QrcodeUtils;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.entity.checkManager.CheckPlanInfo;
import com.aishua.fire.entity.checkManager.CheckPlanReqInfo;
import com.aishua.fire.entity.checkManager.CheckPlanSatisticalInfo;
import com.aishua.fire.entity.checkManager.CheckPointInfo;
import com.aishua.fire.entity.checkManager.CheckPointReqInfo;
import com.aishua.fire.entity.checkManager.CheckPointSatisticalInfo;
import com.aishua.fire.entity.checkManager.CheckRecordDetailsTwoRespInfo;
import com.aishua.fire.entity.checkManager.CheckRecordReq;
import com.aishua.fire.entity.checkManager.CheckRecordRespInfo;
import com.aishua.fire.entity.checkManager.CheckRouteInfo;
import com.aishua.fire.entity.checkManager.CheckRouteReqInfo;
import com.aishua.fire.service.CheckManagerService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "巡检管理", tags = { "巡检管理相关接口" })
@RestController
@Slf4j
public class CheckManagerController {

	
	@Value("${server.path}")
	private String path;
	
	@Value("${project.imgupload.rootPath}")
	private String mImagesPlanPath1;
	
	@Value("${project.imgreq.rootPath}")
	private String mImagesPlanPath;
	
	@Autowired
	private CheckManagerService checkManagerService;
	
	
	@ApiOperation(value = "分页查询计划接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPlan)
	@GetMapping(value = { "/check/plan" })
	public ResponseEntity<JsonArrayResult<CheckPlanInfo>> checkPlanList(
			@RequestParam(name = "num") String num, @RequestParam(name = "name") String name,
			 @RequestParam(name = "type") String type,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		List<CheckPlanInfo> searchList = checkManagerService.searchPlanList(num, name,type,pageNumber, pageSize);
		int total = checkManagerService.searchPlanTotal(num, name,type);
		log.info("分页查询计划条数：{}",total);
		JsonArrayResult<CheckPlanInfo> r = JsonArrayResult.makeSuccessResult(searchList, total);
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加巡检计划接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPlan)
	@PostMapping(value = { "/check/plan" })
	public ResponseEntity<JsonObjectResult> addPlan(
		@RequestBody @Valid CheckPlanReqInfo checkPlan){
		//校验巡检路线
		boolean b =checkManagerService.findByPlanNum(checkPlan.getNum());
		JsonObjectResult r=null;
		if(b) {
			if("0".equals(checkPlan.getType())) {
				checkPlan.setTime("30");
			}else if("1".equals(checkPlan.getType())) {
				if("1".equals(checkPlan.getInterval())) {
					checkPlan.setTime("4");
				}else if("2".equals(checkPlan.getInterval())) {
					checkPlan.setTime("2");
				}
			}else if ("2".equals(checkPlan.getType())) {
				checkPlan.setTime("1");
			}
			checkManagerService.addCheckPlan(checkPlan);
			r = JsonObjectResult.makeSuccessResult(null);
		}else {
			r = JsonObjectResult.makeErrorResult("检查计划编号重复");
		}
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除巡检计划接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPlan)
	@DeleteMapping(value = { "/check/plan" })
	public ResponseEntity<JsonObjectResult> delPlan(@RequestBody @Valid CheckPlanReqInfo checkPlan){
		checkManagerService.delCheckPlan(checkPlan.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改巡检计划接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPlan)
	@PutMapping(value = { "/check/plan" })
	public ResponseEntity<JsonObjectResult> updatePlan(
			@RequestBody @Valid CheckPlanReqInfo checkPlan){
		checkManagerService.updateCheckPlan(checkPlan);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	
	@ApiOperation(value = "分页查询巡检路线接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRoute)
	@GetMapping(value = { "/check/route" })
	public ResponseEntity<JsonArrayResult<CheckRouteInfo>> checkRouteList(
			@RequestParam(name = "route_name") String route_name, @RequestParam(name = "route_num") String route_num,
			 @RequestParam(name = "duty_name") String duty_name,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		List<CheckRouteInfo> searchList = checkManagerService.searchRouteList(route_name, route_num,duty_name,pageNumber, pageSize);
		int total = checkManagerService.searchRouteTotal(route_name, route_num,duty_name);		
		JsonArrayResult<CheckRouteInfo> r = JsonArrayResult.makeSuccessResult(searchList, total);
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加巡检路线接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRoute)
	@PostMapping(value = { "/check/route" })
	public ResponseEntity<JsonObjectResult> addCheckRoute(
			@RequestBody @Valid CheckRouteReqInfo checkRoute){
		//校验巡检路线
		boolean b =checkManagerService.findByRouteNum(checkRoute.getRoute_num());
		JsonObjectResult r=null;
		if(b) {
			checkManagerService.addCheckRoute(checkRoute);
			r = JsonObjectResult.makeSuccessResult(null);
		}else {
			r = JsonObjectResult.makeErrorResult("检查路线编号重复");
		}
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改巡检路线接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRoute)
	@PutMapping(value = { "/check/route" })
	public ResponseEntity<JsonObjectResult> updateCheckRoute(
			@RequestBody @Valid CheckRouteReqInfo checkRoute){
		checkManagerService.updateCheckRoute(checkRoute);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除巡检路线接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRoute)
	@DeleteMapping(value = { "/check/route" })
	public ResponseEntity<JsonObjectResult> delRoute(@RequestBody @Valid CheckRouteReqInfo checkRoute ){
		checkManagerService.delRoute(checkRoute.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	
	@ApiOperation(value = "分页查询巡检点接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPoint)
	@GetMapping(value = { "/check/point" })
	public ResponseEntity<JsonArrayResult<CheckPointInfo>> checkPointList(
			@RequestParam(name = "num") String num, @RequestParam(name = "name") String name,
			 @RequestParam(name = "location") String location, @RequestParam(name = "route_id") String route_id,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		List<CheckPointInfo> searchList = checkManagerService.searchPointList(num, name,location,pageNumber, pageSize,route_id);
		int total = checkManagerService.searchPointTotal(num, name,location,route_id);		
		JsonArrayResult<CheckPointInfo> r = JsonArrayResult.makeSuccessResult(searchList, total);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加巡检点接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPoint)
	@PostMapping(value = { "/check/point" })
	public ResponseEntity<JsonObjectResult> addCheckPoint(
			@RequestBody @Valid CheckPointReqInfo checkPoint){
		//校验巡检点
		boolean b =checkManagerService.findByPointNum(checkPoint.getNum());
		JsonObjectResult r=null;
		if(b) {
			checkManagerService.addCheckPoint(checkPoint);
			//生成二维码
			String url =checkPoint.getNum();
			String picName =createQRcode(checkPoint.getNum(), url,checkPoint.getName());
			checkManagerService.updatePointImg(picName,checkPoint.getNum());
			 r= JsonObjectResult.makeSuccessResult(null);
		}else {
			r = JsonObjectResult.makeErrorResult("检查点编号重复");
		}
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除巡检点接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPoint)
	@DeleteMapping(value = { "/check/point" })
	public ResponseEntity<JsonObjectResult> delPoint(@RequestBody @Valid CheckPointReqInfo checkPoint ){
		checkManagerService.delPoint(checkPoint.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改巡检点接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkPoint)
	@PutMapping(value = { "/check/point" })
	public ResponseEntity<JsonObjectResult> updateCheckPoint(
			@RequestBody @Valid CheckPointReqInfo checkPoint){
		checkManagerService.updateCheckPoint(checkPoint);
		// 修改对应二维码
		String url = checkPoint.getNum();
		String picName=createQRcode(checkPoint.getNum(), url,checkPoint.getName());
		checkManagerService.updatePointImg(picName,checkPoint.getNum());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@ApiOperation(value = "巡检计划统计分析")
	@GetMapping(value = { "/check/planTotal" })
	public ResponseEntity<JsonArrayResult<CheckPlanSatisticalInfo>> planStatistical(
			@RequestParam(name = "start_time") String start_time, @RequestParam(name = "end_time") String end_time,
			 @RequestParam(name = "duty_name") String duty_name, @RequestParam(name = "name") String name,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		List<CheckPlanSatisticalInfo> searchList = checkManagerService.planStatisticalList(start_time,end_time,duty_name, name,pageNumber, pageSize);
		int total = checkManagerService.planStatisticalTotal(start_time,end_time,duty_name, name);		
		JsonArrayResult<CheckPlanSatisticalInfo> r = JsonArrayResult.makeSuccessResult(searchList, total);
		return ResponseEntity.ok(r);
	}
	
	
	@ApiOperation(value = "巡检点统计分析")
	@GetMapping(value = { "/check/pointTotal" })
	public ResponseEntity<JsonArrayResult<CheckPointSatisticalInfo>> pointStatistical(
			@RequestParam(name = "start_time") String start_time, @RequestParam(name = "end_time") String end_time,
			 @RequestParam(name = "duty_name") String duty_name, @RequestParam(name = "point_name") String point_name,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		List<CheckPointSatisticalInfo> searchList = checkManagerService.pointStatisticalList(start_time,end_time,duty_name, point_name,pageNumber, pageSize);
		int total = checkManagerService.pointStatisticalTotal(start_time,end_time,duty_name, point_name);		
		JsonArrayResult<CheckPointSatisticalInfo> r = JsonArrayResult.makeSuccessResult(searchList, total);
		return ResponseEntity.ok(r);
	}
	
	
	/**
	 * 生成二维码
	 * @param code
	 * @param url
	 * @param devicesName
     * @return
     */
	private String createQRcode(String code, String url,String name) {
		// 写入文字内容
		String writing = "pointNum:" + code+ "/" + "pointName:" + name; ;
		String picName = QrcodeUtils.createQRCode(url, writing, mImagesPlanPath1, code);
		picName ="http://"+ path+":"+"8080" + mImagesPlanPath + picName;
		return picName;
	}
	
	@ApiOperation(value = "分页巡检记录接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRecord)
	@GetMapping(value = { "/check/record" })
	public ResponseEntity<JsonArrayResult<CheckRecordRespInfo>> checkRecordList(
			@RequestParam(name = "userName") String userName, @RequestParam(name = "pointName") String pointName,
			 @RequestParam(name = "situation") String situation,@RequestParam(name = "start_time") String start_time,
			 @RequestParam(name = "end_time") String end_time,
			@RequestParam(name = "pageNumber") int pageNumber,@RequestParam(name = "pageSize") int pageSize){
		JsonArrayResult<CheckRecordRespInfo> r=null;
		List<CheckRecordRespInfo> searchList = checkManagerService.searchRecordList(userName,pointName,situation,start_time,end_time,pageNumber, pageSize);
		if(searchList.size()>0) {
			PageInfo<CheckRecordRespInfo> pages = new PageInfo<CheckRecordRespInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int)pages.getTotal());
		}else {
			r=JsonArrayResult.makeSuccessResult(searchList,0);
		}	
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "导出巡检记录接口")
	@ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRecord)
	@PostMapping(value = { "/check/exportRecord" })
	public ResponseEntity<JsonObjectResult> exportRecord(
			@RequestBody @Valid CheckRecordReq record ){
		JsonObjectResult r = null;
		if(record.getId().length==0) {
			r=JsonObjectResult.makeErrorResult("未选中记录");
		}else {
			String exportUrl  = checkManagerService.exportRecord(record.getId());
			r=JsonObjectResult.makeSuccessResult(exportUrl);
		}
		return ResponseEntity.ok(r);
	}
	
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "查询巡检记录详情接口")
    @ModuleService(totalModule=BigMenuConstants.checkManager,moduleName=ModuleConstants.checkRecord)
    @GetMapping(value = { "/check/exportDetails" })
    public ResponseEntity<JsonObjectResult> recordDetails(
            @RequestParam(name = "id") String id){
    
        CheckRecordDetailsTwoRespInfo details  = checkManagerService.recordDetails(id);
        JsonObjectResult r=JsonObjectResult.makeSuccessResult(details);
        return ResponseEntity.ok(r);
    }
	    
}
